package com.chinacoal.hr.levelimpl.dao;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chinacoal.hr.levelapi.entity.Employee;
import com.chinacoal.hr.levelimpl.mapper.EmpOrgMapper;
import com.chinacoal.hr.levelimpl.mapper.EmployeeMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author: YunTao.Li
 * @create: 2019-12-04 18:06
 * @description:
 **/
@Component
public class EmployeeDao {
    @Resource
    public EmployeeMapper employeeMapper;

    @Resource
    public EmpOrgMapper empOrgMapper;

    /**
     * 考勤本地员工列表带分页
     *
     * @param condition :
     * @return : com.baomidou.mybatisplus.core.metadata.IPage<com.chinacoal.hr.levelimpl.entity.AttendanceAppeal>
     * @author : YunTao.Li
     * @date : 2019/11/20 2019/11/20
     */
    public IPage<Employee> getEmployeeslList(Map<String, Object> condition) {
        Long currentPage = (Long) condition.get("page");
        Long currentPageSize = (Long) condition.get("limit");
        IPage<Employee> page = new Page(currentPage, currentPageSize);

        String empId = condition.get("empId") != null ? condition.get("empId").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String empName = condition.get("empName") != null ? condition.get("empName").toString() : "";
        String orgId = condition.get("orgId") != null ? condition.get("orgId").toString() : "";
        String orgCode = condition.get("orgCode") != null ? condition.get("orgCode").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(empId)) {
            queryWrapper.eq("EMPID", empId);
        }
        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMPCODE", empCode);
        }
        if (StringUtils.isNotBlank(empName)) {
            queryWrapper.eq("EMPNAME", empName);
        }
        if (StringUtils.isNotBlank(orgId)) {
            queryWrapper.eq("ORGID", orgId);
        }
        if (StringUtils.isNotBlank(orgCode)) {
            queryWrapper.eq("ORG_CODE", orgCode);
        }

        queryWrapper.orderByDesc("ORG_CODE");
        queryWrapper.orderByDesc("DEPT_CODE");
        queryWrapper.orderByAsc("EMPID");


        IPage<Employee> employees = employeeMapper.selectPage(page, queryWrapper);

        return employees;
    }

    /**
     * 考勤本地员工列表不带分页
     *
     * @param condition :
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.Employee>
     * @author : YunTao.Li
     * @date : 2020/7/17 2020/7/17
     */
    public List<Employee> getEmployeeslListWithOutPage(Map<String, Object> condition) {
        String empId = condition.get("empId") != null ? condition.get("empId").toString() : "";
        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";
        String empName = condition.get("empName") != null ? condition.get("empName").toString() : "";
        String orgId = condition.get("orgId") != null ? condition.get("orgId").toString() : "";

        QueryWrapper queryWrapper = new QueryWrapper();

        if (StringUtils.isNotBlank(empId)) {
            queryWrapper.eq("EMPID", empId);
        }
        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMPCODE", empCode);
        }
        if (StringUtils.isNotBlank(empName)) {
            queryWrapper.eq("EMPNAME", empName);
        }
        if (StringUtils.isNotBlank(orgId)) {
            queryWrapper.eq("org_id", orgId);
        }

        queryWrapper.orderByDesc("EMPNAME");


        List<Employee> employees = employeeMapper.selectList(queryWrapper);

        return employees;
    }

    /**
     * 模糊查询
     *
     * @param condition : {empName}
     * @return : java.util.List<com.chinacoal.hr.levelimpl.entity.Employee>
     * @author : YunTao.Li
     * @date : 2019/12/11 2019/12/11
     */
    public List<Employee> getEmployeeslListFuzzy(Map<String, Object> condition) throws Throwable {
        try {
            String empName = condition.get("empName") != null ? condition.get("empName").toString() : "";

            QueryWrapper queryWrapper = new QueryWrapper();

            if (StringUtils.isNotBlank(empName)) {
                queryWrapper.like("EMPNAME", empName);
            }

            List<Employee> employees = this.employeeMapper.selectList(queryWrapper);
            return employees;
        } catch (Throwable e) {
            e.printStackTrace();
            throw e;
        }
    }

    /**
     * 根据条件获得唯一的员工信息
     *
     * @param condition :
     * @return : com.chinacoal.hr.levelimpl.entity.Employee
     * @author : YunTao.Li
     * @date : 2020/1/7 2020/1/7
     */
    public Employee getEmployeeByCondition(Map<String, Object> condition) throws Throwable {
        QueryWrapper queryWrapper = new QueryWrapper();

        String empCode = condition.get("empCode") != null ? condition.get("empCode").toString() : "";


        if (StringUtils.isNotBlank(empCode)) {
            queryWrapper.eq("EMPCODE", empCode);
        }

        queryWrapper.last("limit 1");

        Employee employee = this.employeeMapper.selectOne(queryWrapper);
        return employee;
    }
}
